package com.party.core.service.gift.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.core.dao.read.gift.GiftReadDao;
import com.party.core.dao.write.gift.GiftWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.gift.Gift;
import com.party.core.model.gift.GiftListOut;
import com.party.core.model.gift.GiftListOutput;
import com.party.core.service.gift.IGiftService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: Administrator.
 * @Description: 礼包
 * @Date:Created in 2017/11/15 0015 上午 10:36.
 */
@Service
public class GiftService implements IGiftService {
    @Autowired
    private GiftReadDao giftReadDao;
    @Autowired
    private GiftWriteDao giftWriteDao;

    @Override
    public String insert(Gift t) {
        BaseModel.preInsert(t);
        boolean result = giftWriteDao.insert(t);
        if (result) {
            return t.getId();
        }
        return null;
    }

    @Override
    public boolean update(Gift t) {
        t.setUpdateDate(new Date());
        return giftWriteDao.update(t);
    }

    @Override
    public boolean deleteLogic(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return giftWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return giftWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return giftWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return giftWriteDao.batchDelete(ids);
    }

    @Override
    public Gift get(String id) {
        return giftReadDao.get(id);
    }

    @Override
    public List<Gift> listPage(Gift t, Page page) {
        return giftReadDao.listPage(t, page);
    }

    @Override
    public List<Gift> list(Gift t) {
        return giftReadDao.listPage(t, null);
    }

    @Override
    public List<Gift> batchList(Set<String> ids, Gift t, Page page) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.EMPTY_LIST;
        }
        return giftReadDao.batchList(ids, new HashedMap(), page);
    }

    /**
     * web端查询
     *
     * @param gift
     * @param params
     * @param page
     * @return
     */
    @Override
    public List<GiftListOutput> webListPage(GiftListOutput gift, Map<String, Object> params, Page page) {
        return giftReadDao.webListPage(gift, params, page);
    }

    @Override
    public List<Gift> findByTargetId(String targetId, String type) {
        return giftReadDao.findByTargetId(targetId, type);
    }

    @Override
    public List<GiftListOut> findAllByMemberId(String memberId, Integer isOrder, Page page) {
        return giftReadDao.findAllByMemberId(memberId, isOrder, page);
    }

    @Override
    public List<String> getOverdue(String memberId) {
        return giftReadDao.getOverdue(memberId);
    }
}
